<?php 
/*
 module:		毕业生信息
 create_time:	2020-07-21 14:43:53
 author:		
 contact:		
*/

namespace app\api\controller;

use app\api\service\GraduationService;
use app\api\model\Graduation as GraduationModel;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;

class Graduation extends Common {


	/**
	* @api {get} /Graduation/index 01、首页数据列表
	* @apiGroup Graduation
	* @apiVersion 1.0.0
	* @apiDescription  首页数据列表
	* @apiParam (输入参数：) {int}     		[limit] 每页数据条数（默认20）
	* @apiParam (输入参数：) {int}     		[page] 当前页码
	* @apiParam (输入参数：) {string}		[name] 姓名 
	* @apiParam (输入参数：) {string}		[u_dangan] 单位是否接收档案 

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.data 返回数据
	* @apiParam (成功返回参数：) {string}     	array.data.list 返回数据列表
	* @apiParam (成功返回参数：) {string}     	array.data.count 返回数据总数
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":""}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" 201","msg":"查询失败"}
	*/
	function index(){
		$limit  = $this->request->get('limit', 20, 'intval');
		$page   = $this->request->get('page', 1, 'intval');

		$where = [];
		$where['name'] = $this->request->get('name', '', 'serach_in');
		$where['student_num'] = $this->request->get('student_num', '', 'serach_in');
		$where['u_dangan'] = $this->request->get('u_dangan', '', 'serach_in');

		$field = '*';
		$orderby = 'graduation_id desc';

		$res = GraduationService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
		return $this->ajaxReturn($this->successCode,'返回成功',htmlOutList($res));
	}

	/**
	* @api {post} /Graduation/add 02、添加
	* @apiGroup Graduation
	* @apiVersion 1.0.0
	* @apiDescription  添加
	* @apiParam (输入参数：) {string}			name 姓名 (必填) 
	* @apiParam (输入参数：) {int}				student_num 学号 (必填) 
	* @apiParam (输入参数：) {string}			major 专业 (必填) 
	* @apiParam (输入参数：) {string}			class 班级 (必填) 
	* @apiParam (输入参数：) {int}				tel 毕业生联系电话 (必填) 
	* @apiParam (输入参数：) {string}			g_des 就业方向 (必填) 
	* @apiParam (输入参数：) {string}			u_name 单位名称 (必填) 
	* @apiParam (输入参数：) {string}			o_daima 组织机构代码 
	* @apiParam (输入参数：) {string}			u_nature 单位性质 
	* @apiParam (输入参数：) {string}			u_vocation 单位行业 (必填) 
	* @apiParam (输入参数：) {string}			u_location 单位所在地 
	* @apiParam (输入参数：) {string}			u_positiion 工作职位类别 (必填) 
	* @apiParam (输入参数：) {string}			u_lingkman 单位联系人 
	* @apiParam (输入参数：) {int}				linkman_tel 单位联系人电话 
	* @apiParam (输入参数：) {string}			u_address 单位地址（精确到门牌号） 
	* @apiParam (输入参数：) {int}				u_p_c 单位邮编 
	* @apiParam (输入参数：) {string}			process_create_time 毕业表单提交时间 
	* @apiParam (输入参数：) {string}			u_dangan 单位是否接收档案 (必填) 
	* @apiParam (输入参数：) {string}			bq_unitname 报到证签往单位名称 
	* @apiParam (输入参数：) {string}			bq_unitaddr 报到证签往单位所在地 
	* @apiParam (输入参数：) {string}			rd_unitname 接收档案单位名称 
	* @apiParam (输入参数：) {string}			d_unitp_c 档案转寄单位邮编 
	* @apiParam (输入参数：) {string}			d_unitaddr 档案转寄单位地址（精确到门牌号） 
	* @apiParam (输入参数：) {string}			img 三方协议或就业合同照片 
	* @apiParam (输入参数：) {int}				user_id 钉钉uese_id 

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码  201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.msg 返回成功消息
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":"操作成功"}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" 201","msg":"操作失败"}
	*/
	function add(){
		$postField = 'name,student_num,major,class,tel,g_des,u_name,o_daima,u_nature,u_vocation,u_location,u_positiion,u_lingkman,linkman_tel,u_address,u_p_c,process_create_time,u_dangan,bq_unitname,bq_unitaddr,rd_unitname,d_unitp_c,d_unitaddr,img,user_id';
		$data = $this->request->only(explode(',',$postField),'post',null);
		$res = GraduationService::add($data);
		return $this->ajaxReturn($this->successCode,'操作成功',$res);
	}

	/**
	* @api {get} /Graduation/view 05、查看详情
	* @apiGroup Graduation
	* @apiVersion 1.0.0
	* @apiDescription  查看详情
	
	* @apiParam (输入参数：) {string}     		graduation_id 主键ID

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.data 返回数据详情
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":""}
	* @apiErrorExample {json} 02 失败示例
	* {"status":"201","msg":"没有数据"}
	*/
	function view(){
		$data['graduation_id'] = $this->request->get('graduation_id','','serach_in');
		$field='graduation_id,name,student_num,major,class,tel,g_des,u_name,o_daima,u_nature,u_vocation,u_location,u_positiion,u_lingkman,linkman_tel,u_address,u_p_c,process_create_time,u_dangan,bq_unitname,bq_unitaddr,rd_unitname,d_unitp_c,d_unitaddr,img,user_id';
		$res  = checkData(GraduationModel::field($field)->where($data)->find());
		return $this->ajaxReturn($this->successCode,'返回成功',$res);
	}



	function get_access_token(){
		 $authCode=$_GET["authCode"];
		 $appkey='dinge1iuklbh28b9hojg';
         $appSecret='q1yR9jAAi2aZbPu8uh63QeLkEGe3O8tW9EiGAsaHN8gJaNfqrynmSoO_392jeWkG';
         $url='https://oapi.dingtalk.com/gettoken?appkey='.$appkey.'&appsecret='.$appSecret;
         $access_token=json_decode(file_get_contents($url),true)["access_token"];//获取accesstoken

         $url="https://oapi.dingtalk.com/user/getuserinfo?access_token=".$access_token."&code=".$authCode;
         $msg=json_decode(file_get_contents($url),true);
         $user_id=$msg["userid"];//获取user_id
         $data["userid"]=$user_id;
         $data["name"]=$msg["name"];//获取名称
         $url="https://oapi.dingtalk.com/user/get?access_token=".$access_token."&userid=".$user_id;
         $img_url=json_decode(file_get_contents($url),true);
         $data["head_img"]=$img_url["avatar"];//获取头像
         
         
         return $this->ajaxReturn($this->successCode,"返回成功",$data);
	}



}

